<?php
/**
 * Created by PhpStorm.
 * User: jhon
 * Date: 2018/6/12
 * Time: 16:09
 */

namespace app\api\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Request;
class Users extends Controller
{

    protected $role = 3;

    //登录
    protected $rule =   [
        'id'=>'require',
        'acc_name'  => 'require|max:25',
        'acc_pwd'  => 'require|max:36',
        'age'   => 'number|between:1,120',
        'email' => 'email',
    ];
    ///登录
    protected $message  =   [
        'id.require' => 'id必填参数',
        'acc_name.require' => '用户名必填参数',
        'acc_name.max'     => '用户名最多不能超过25个字符',
        'acc_pwd.require' => '密码必填参数',
        'acc_pwd.max'     => '密码最多不能超过36个字符',
        'age.number'   => '年龄必须是数字',
        'age.between'  => '年龄只能在1-120之间',
        'email'        => '邮箱格式错误',
    ];

    /**
     * @desc 登录
     * @param ['id';'','user_name':'','user_paw']
     * @return ['error':'','message':'','data':'',]
     * data = ['pageindex':'','pagesize':'','content':'',pagecount:]
     */
    public function login1(){
        $message = '';
        $Request = Request::instance();
        $param = $Request->param();
        $data = [
            'id'=>input('id'),
            'acc_name'=>input('acc_name'),
            'acc_pwd'=>input('acc_pwd'),
        ];
        $validate = new Validate($this->rule,$this->message);
        $result   = $validate->check($data);
        if(!$result){
            $message = $validate->getError();
            return json(returnRest($message,[],100001));
        }
        unset($data['id']);
        $data = $this->checklogin($data);
        $content = [
            'pageindex'=>1,
            'pagesize'=>1,
            'pagecount'=>1,
            'content'=>$data
        ];
        if(empty($data)){
            $message = '用户名或密码错误';
            return json(returnRest($message,$content,101002));
        }else{
            return json(returnRest($message,$content,0));
        }

    }

    /**
     * 登录db
    */
    public function checklogin($data){

        $resouce = Db::table('user_acc')->field('user_acc_sn,user_guid,user_email,acc_name,org_code,mark,user_role')->where($data)->select();
        return $resouce;
    }

    public function login(){
        $data = [
            'id'=>input('id'),
            'name'=>input('name'),
            'pwd'=>input('pwd'),
        ];
        $rule = [
            'id'=>'require',
            'name'=> 'require|max:25',
            'pwd'=> 'require|max:25',
        ];
        $validate = new Validate($rule);
        $result   = $validate->check($data);
        if(!$result){
            $message = $validate->getError();
            return json(returnRest($message,[],100001));
        }
        unset($data['id']);
        $data = Db::table('padaccount')
            ->field('id,name,type,org_code,login_mark')
            ->where($data)
            ->select();

        if(empty($data)){
            $message = '用户名或密码错误';
            return json(returnRest($message,[],101002));
        }else{

            if($data[0]['login_mark'] == 0){
                $message = '用户已登录';
                return json(returnRest($message,[],101004));
            }

//            修改登录标识
            $this->updloginmark($data[0]['id'],0);
            $typcode = $this ->getdevcountorg($data[0]['org_code']);
            $devcode = $this ->getdevbyorg($data[0]['org_code']);
            $orgcode = $this ->getorgdata($data[0]['org_code']);

            $content = [
                'pageindex'=>1,
                'pagesize'=>1,
                'pagecount'=>1,
                'typcode'=>$typcode,
                'devcode'=>$devcode,
                'orgcode'=>$orgcode,
                'content'=>$data,
                'heartbeat'=>config('appconfig.Heartbeat')
            ];
            $message ='';
            return json(returnRest($message,$content,0));
        }
//        return json($resouce);
    }

    public function loginout(){
        $data = [
            'id'=>input('id'),
            'name'=>input('name'),
        ];
        $rule = [
            'id'=>'require',
            'name'=> 'require|max:25',
        ];

        $validate = new Validate($rule);
        $result   = $validate->check($data);
        if(!$result){
            $message = $validate->getError();
            return json(returnRest($message,[],100001));
        }

        unset($data['id']);
        $data = Db::table('padaccount')
            ->field('id,name,type,org_code,login_mark')
            ->where($data)
            ->select();
        if(empty($data)){
            $message = '用户名不存在';
            return json(returnRest($message,[],101003));
        }else{
            $loginmarl =$this->updloginmark($data[0]['id'],1);

            if($loginmarl){
                $message = '状态修改成功';
                return json(returnRest($message,[],0));
            }else{
                $message = '状态修改失败';
                return json(returnRest($message,[],101005));

            }
        }
    }


    /**
     * 获取设备总数
     * @param $org_code
     * @return int|string
     */
    public function getdevcountorg($org_code){
        $typecount =Db::table('device')
            ->where('org_code',$org_code)
            ->count();
        return $typecount;
    }

    /**
     * 获取设备列表
     * @param $org_code
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getdevbyorg($org_code){
        $typecount =Db::table('device')
            ->where('org_code',$org_code)
            ->where('dev_status',1)
            ->select();
        return $typecount;
    }


    /**
     * 登录标识修改
     * @param $id
     * @param $loginmark
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updloginmark($id,$loginmark){
        $typecount =Db::table('padaccount')
            ->where('id',$id)
            ->update(['login_mark'=>$loginmark]);
        return $typecount;
    }

    /**
     * 获取机构数据
     * @param $org_code
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getorgdata($org_code){
        $result  = Db::table('organization')->where('org_code',$org_code)->select();
        return $result;
    }


    public function updonlinetime($username,$create_time){

        $typecount =Db::table('padaccount')
            ->where('name',$username)
            ->update(['last_online_time'=>$create_time]);

        if(empty($typecount)){
            return false;
        }else{
            return true;
        }
//        return $typecount;
    }


}